package com.qfedu.hotelssm.service.impl;

import com.qfedu.hotelssm.dao.MenuDao;
import com.qfedu.hotelssm.entity.Menu;
import com.qfedu.hotelssm.entity.User;
import com.qfedu.hotelssm.service.intf.MenuService;
import com.qfedu.hotelssm.vo.TreeMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author truman
 * @date 2022/9/1
 * @description 菜单业务层实现
 */
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao dao;

    @Override
    public Map all(HttpSession session) {
        User user = (User) session.getAttribute("user");
        if (Objects.nonNull(user)) {
            //用户已登录，组装json数据
            List<Menu> menuList = dao.all(user.getId());
            List<TreeMenu> list1 = new ArrayList<>();
            for (Menu m1 : menuList) {
                if (m1.getLevel() == 1) {
                    List<TreeMenu> list2 = new ArrayList<>();
//                    List<Menu> menuList1 = dao.all(user.getId());
                    for (Menu m2 : menuList) {
                        if (m2.getPid() == m1.getId()) {
                            List<TreeMenu> list3 = new ArrayList<>();
//                            List<Menu> menuList2 = dao.all(user.getId());
                            for (Menu m3 : menuList) {
                                if (m3.getPid() == m2.getId()) {
                                    list3.add(new TreeMenu(m3.getTitle(), m3.getHref(), m3.getIcon(), m2.getTarget(), null));
                                }
                            }
                            list2.add(new TreeMenu(m2.getTitle(), m2.getHref(), m2.getIcon(), m2.getTarget(), list3));
                        }
                    }
                    list1.add(new TreeMenu(m1.getTitle(), m1.getHref(), m1.getIcon(), m1.getTarget(), list2));
                }
            }
            Map<String, String> map1 = new LinkedHashMap<>();
            map1.put("title", "首页");
            map1.put("href", "page/welcome-1.html?t=1");
            Map<String, String> map2 = new LinkedHashMap<>();
            map2.put("title", "");
            map2.put("image", "images/logo.png");
            map2.put("href", "");
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("homeInfo", map1);
            map.put("logoInfo", map2);
            map.put("menuInfo", list1);
            return map;
        } else {
            return null;
        }
//        if (Objects.nonNull(user)) {
//            //用户已登录，组装json数据
//            List<Menu> menuList = dao.all(user.getId());
//            List<TreeMenu> list = new ArrayList<>();
//            menuList.forEach(m -> {
//                TreeMenu menu = new TreeMenu(m.getId(),m.getTitle(), m.getHref(), m.getIcon(), m.getTarget(), new ArrayList<>());
//                if (m.getLevel() == 1) {
//                    list.add(menu);
//                } else if (m.getLevel() == 2) {
//                    for (TreeMenu treeMenu : list) {
//                        if (m.getPid() == treeMenu.getId()) {
//                            treeMenu.getChild().add(menu);
//                        }
//                    }
//                } else {
//                    for (TreeMenu treeMenu : list) {
//                        for (TreeMenu twoMenu : treeMenu.getChild()) {
//                            if (m.getPid() == twoMenu.getId()) {
//                                twoMenu.getChild().add(menu);
//                            }
//                        }
//                    }
//                }
//            });
//
//            Map<String, String> map1 = new HashMap<>();
//            map1.put("title", "首页");
//            map1.put("href", "page/welcome-1.html?t=1");
//            Map<String, String> map2 = new HashMap<>();
//            map2.put("title", "");
//            map2.put("image", "images/logo.png");
//            map2.put("href", "images/logo.png");
//            Map<String, Object> map = new LinkedHashMap<>();
//            map.put("homeInfo", map1);
//            map.put("logoInfo", map2);
//            map.put("menuInfo", list);
//            return map;
//        }
//        return null;
    }
}
